#include "tcmath.h"

TcMath::TcMath()
{

}

std::string TcMath::bcadd(const std::string &left, const std::string &right, int scale)
{
  return ::bcadd(left, right, scale);

}

QString TcMath::bcadd(const QString &left, const QString &right, int scale)
{
    return QString::fromStdString(
                ::bcadd(left.toStdString(), right.toStdString(), scale));
}

QString TcMath::currencyText(const QString &number)
{
    //static QString str2 = C("仟佰拾万仟佰拾亿仟佰拾万仟佰拾元");
    static const QString str2 = QString::fromUtf8("\344\273\237\344\275\260\346\213\276\344\270\207\344\273\237\344\275\260\346\213\276\344\272\277\344\273\237\344\275\260\346\213\276\344\270\207\344\273\237\344\275\260\346\213\276\345\205\203");
    //static QString str3 = C("零壹贰叁肆伍陆柒捌玖");
    static const QString str3 = QString::fromUtf8("\351\233\266\345\243\271\350\264\260\345\217\201\350\202\206\344\274\215\351\231\206\346\237\222\346\215\214\347\216\226");
    static const QString strYiWan = QString::fromUtf8("\344\272\277\344\270\207");//= C("亿万");
    static const QString strZero = QString::fromUtf8("\351\233\266");// = C("零");
    static const QString strZero2 = QString::fromUtf8("\351\233\266\351\233\266");// = C("零零");
    static const QString strYuan = QString::fromUtf8("\xe5\x85\x83");// = C("元");
    static const QString strZheng = QString::fromUtf8("");// = C("整");
    static const QString strJiao = QString::fromUtf8("\350\247\222");// = C("角");
    static const QString strFen = QString::fromUtf8("\345\210\206");// = C("分");
    static const QString strJiaoZheng = QString::fromUtf8("\350\247\222\346\225\264");// = C("角整");

    QString str = bcadd(number, QString("0.00"), 2);
    str.replace("-", "");

    int cnt = str3.size();
    QStringList str3arr;
    for (int i = 0; i < cnt; i++)
    {
        str3arr << str3.mid(i, 1);
    }
    int len2 = str.size() - 3;
    QString str22 = str2.mid(str2.size() - len2);

    QString str4 = "";
    for (int i = 0; i < len2; i++)
    {
        int j = str.mid(i, 1).toInt();
        QString word_unit = str22.mid(i, 1);
        if (strYiWan.indexOf(word_unit) > -1)
        {
            if (j == 0)
            {
                str4 += word_unit;
            }
            else
            {
                str4 += str3arr[j] + word_unit;
            }
        }
        else
        {
            if (j == 0)
            {
                str4 += strZero;//C("零");
            }
            else
            {
                str4 += str3arr[j] + word_unit;
            }
        }
    }
    int pos;
    while ((pos = str4.indexOf(strZero2)) > -1)
    {
        str4 = str4.mid(0, pos) + str4.mid(pos + 1);
    }
    if (str4.right(1) == strZero)
    {
        if (str4.size() > 1)
        {
            str4 = str4.mid(0, str4.size() - 1) + strYuan;//C("元");
        }
        else
        {
            str4 += strYuan;//C("元");
        }
    }

    if (str.right(2) == "00")
    {
        str4 += strZheng;//C("整");
    }
    else if (str.right(1) == "0")
    {
        // 零分
        int j = str.right(2).left(1).toInt();
        str4 += str3arr[j] + strJiaoZheng;
    }
    else if (str.right(2).left(1) == "0")
    {
        // 零角
        int j = str.right(1).toInt();
        str4 += strZero + str3arr[j] + strFen;
    }
    else
    {
        int j = str.right(2).left(1).toInt();
        str4 += str3arr[j] + strJiao;
        j = str.right(1).toInt();
        str4 += str3arr[j] + strFen;
    }
    //添加额外


    return str4;
}

